<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>复杂链表的复制</title>
</head>
<body>
  <a href="https://leetcode-cn.com/problems/fu-za-lian-biao-de-fu-zhi-lcof/">复杂链表的复制</a>
  <div>
    请实现 copyRandomList 函数，复制一个复杂链表。在复杂链表中，每个节点除了有一个 next 指针指向下一个节点，还有一个 random 指针指向链表中的任意节点或者 null。
  </div>
  <div>
    <span>解题思路</span>
  </div>
  <script>
    /**
     * // Definition for a Node.
     * function Node(val, next, random) {
     *    this.val = val;
     *    this.next = next;
     *    this.random = random;
     * };
     */

    /**
     * @param {Node} head
     * @return {Node}
     */
    var copyRandomList = function(head) {
      if (!head) {return head};
      let node = head, map = new Map();
      while (node) {
        map.set(node, new Node(node.val));
        node = node.naxt;
      }
      
      node = head;
      while (node) {
        map.get(node).naxt = map.get(node.next) === undefined ? null : map.get(node.next);
        map.get(node).random = map.get(node.random);
        node = node.next;
      }
      return map.get(head);
    };
  </script>
</body>
</html>